package org.chen.wordwave.act;

import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import androidx.appcompat.app.AlertDialog;

import org.chen.wordwave.R;
import org.chen.wordwave.base.BaseActivity;
import org.chen.wordwave.bean.FavoriteWords;
import org.chen.wordwave.bean.Words;
import org.chen.wordwave.init.WordInit;
import org.litepal.LitePal;

import java.util.List;

public class FavoriteWordsAct extends BaseActivity {
    private ListView lv_favorite_words;
    private List<Words> favoriteWordsList;
    private MyFavoriteWordAdapter myFavoriteWordAdapter;
    private TextView tv_no_data;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        favoriteWordsList = (List<Words>) getIntent().getSerializableExtra("favWords");
        if (favoriteWordsList.isEmpty()){
            tv_no_data.setVisibility(View.VISIBLE);
        }
        initData();
    }

    private void initData() {
        myFavoriteWordAdapter = new MyFavoriteWordAdapter();
        lv_favorite_words.setAdapter(myFavoriteWordAdapter);
    }

    @Override
    public Integer getLayoutResId() {
        return R.layout.favorite_words_act;
    }

    @Override
    public void init() {
        lv_favorite_words = findViewById(R.id.lv_favorite_words);
        tv_no_data = findViewById(R.id.tv_no_data);
    }

    private class MyFavoriteWordAdapter extends BaseAdapter {
        @Override
        public int getCount() {
            return favoriteWordsList.size();
        }

        @Override
        public Object getItem(int position) {
            return favoriteWordsList.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            MyViewHolder myViewHolder;
            if (convertView==null){
                myViewHolder = new MyViewHolder();
                convertView = View.inflate(FavoriteWordsAct.this, R.layout.item_favorite_word,null);
                myViewHolder.tv_fav_words_en = convertView.findViewById(R.id.tv_fav_words_en);
                myViewHolder.tv_fav_words_cn = convertView.findViewById(R.id.tv_fav_words_cn);
                myViewHolder.iv_favorite = convertView.findViewById(R.id.iv_favorite);
                convertView.setTag(myViewHolder);
            }
            myViewHolder= (MyViewHolder) convertView.getTag();
            Words words=favoriteWordsList.get(position);
            myViewHolder.tv_fav_words_en.setText(words.getEnglish());
            myViewHolder.tv_fav_words_cn.setText(words.getChinese());
            myViewHolder.iv_favorite.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    AlertDialog.Builder normalDialog = new AlertDialog.Builder(FavoriteWordsAct.this);
                    normalDialog.setIcon(R.mipmap.wring);
                    normalDialog.setTitle(R.string.cancel_favorite);
                    normalDialog.setMessage(R.string.are_you_cancel_favorite);
                    normalDialog.setPositiveButton(getString(R.string.yes),
                            new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog, int which) {
//                                    删除数据库中的数据库
                                    LitePal.deleteAll(FavoriteWords.class,"wordsId = ? and email = ?",String.valueOf(favoriteWordsList.get(position).getId()), WordInit.userInfo.getEmail());
//                                    删除list中的数据
                                    favoriteWordsList.remove(position);
//                                    通知数据改变进行数据刷新
                                    myFavoriteWordAdapter.notifyDataSetChanged();
                                }
                            });
                    normalDialog.setNegativeButton(R.string.close,
                            new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog, int which) {

                                }
                            });
                    // 显示
                    normalDialog.show();

                }
            });
            return convertView;
        }


        class MyViewHolder{
            TextView tv_fav_words_en,tv_fav_words_cn;
            ImageView iv_favorite;


        }
    }
}
